/**
 * @filename:AdminuserController 2022-2-27
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wonder.admin.domain.Adminuser;
import com.wonder.admin.vo.AdminuserVo;
import com.wonder.admin.service.AdminuserService;
import com.wonder.admin.request.AdminuserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.utils.DBUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * 
 * <p>说明： 后台用户API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    2022-2-27
 *
 */
@Api(tags = "后台用户")
@RestController
@RequestMapping("/admin/Admin")
public class AdminuserController {
	
	@Autowired
	private AdminuserService adminuserService;
	
	@Autowired
	private DBUtils dbUtils;
	@ApiOperation(value = "后台用户列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody AdminuserRequest adminuserRequest) {
	    Page<AdminuserVo> page = new Page<AdminuserVo>(adminuserRequest.getPage(), adminuserRequest.getLimit());
	    IPage<AdminuserVo> list = adminuserService.selectAdminuserVoList(adminuserRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
        Map<String,Object> smallTable=new HashMap<>();
        smallTable.put("role_ids",dbUtils.getDropDownList("select role_id,name from sys_role"));
        retMap.put("sql_field_data",smallTable);
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 获取sql语句定义的字段信息
	 * @param   对象参数：Adminuser
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/getFieldList")
	@ApiOperation(value = "获取sql语句定义的字段信息", notes = "作者：Wonder")
	public AjaxResult<Map<String,Object>> getFieldList(){
	    Map<String,Object> smallTable=new HashMap<>();
        smallTable.put("role_ids",dbUtils.getDropDownList("select role_id,name from sys_role"));
        return AjaxResult.success(smallTable);
	}
	
	/**
	 * @explain 修改单个字段值
	 * @param   对象参数：Adminuser
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改单个字段值", notes = "作者：Wonder")
	public AjaxResult<Adminuser> updateExt(@RequestBody Adminuser entity){
		if (null!=entity) {
			boolean rsg = adminuserService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("update.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("update.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("update.not.exist"));
		}
	}
	
	/**
	 * @explain 添加
	 * @param   对象参数：Adminuser
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Adminuser> add(@RequestBody Adminuser entity){

		if (null!=entity) {
		    String validate=adminuserService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = adminuserService.save(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("insert.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("insert.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("insert.parameter.error"));
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取后台用户对象", notes = "作者：Wonder")
	public AjaxResult<AdminuserVo> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed(MessageUtils.message("input.parameter.error"));
		Long user_id=Long.parseLong(map.get("user_id").toString());
	      AdminuserVo adminuser=adminuserService.getVoById(user_id);
		
		if (null!=adminuser ) {
			 return AjaxResult.success(adminuser);
		}else {
			 return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   对象参数：Adminuser
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Adminuser> update(@RequestBody Adminuser entity){
		if (null!=entity) {
		    String validate=adminuserService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = adminuserService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("update.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("update.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("update.not.exist"));
		}
	}
	
	/**
	 * @explain 删除对象
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Adminuser> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		String ids=map.get("user_id").toString();
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i]));//整数数组
		}

	    boolean rsg = adminuserService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success(MessageUtils.message("delete.success"));
	    }else {
		   return AjaxResult.failed(MessageUtils.message("delete.fail"));
	    }
		
	}
	
	
}